package com.ticktactoev2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TickTacToev2_by_eric extends Activity {

	 static int count = (int)(Math.random()*10);
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button  b1 =  (Button) findViewById(R.id.n1);
       Button b2 =  (Button) findViewById(R.id.n2);
       Button b3 =  (Button) findViewById(R.id.n3);
       Button b4 =  (Button) findViewById(R.id.n4);
       Button b5 =  (Button) findViewById(R.id.n5);
       Button b6 =  (Button) findViewById(R.id.n6);
       Button b7 =  (Button) findViewById(R.id.n7);
       Button b8 =  (Button) findViewById(R.id.n8);
       Button b9 =  (Button) findViewById(R.id.n9);
       
       
   TextView  tv = (TextView) findViewById(R.id.TextView01);
         tv.setText("Please tap any the game!");
         b1.setOnClickListener(b1Listener);
         b2.setOnClickListener(b2Listener);
         b3.setOnClickListener(b3Listener);
         b4.setOnClickListener(b4Listener);
         b5.setOnClickListener(b5Listener);
         b6.setOnClickListener(b6Listener);
         b7.setOnClickListener(b7Listener);
         b8.setOnClickListener(b8Listener);
         b9.setOnClickListener(b9Listener);
         
         
      
    }
    private OnClickListener b1Listener = new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked
        	 Button  b1 =  (Button) findViewById(R.id.n1);
        	b1.setText(xoro());
        	  Button b2 =  (Button) findViewById(R.id.n2);
              Button b3 =  (Button) findViewById(R.id.n3);
              Button b4 =  (Button) findViewById(R.id.n4);
              Button b5 =  (Button) findViewById(R.id.n5);
              Button b6 =  (Button) findViewById(R.id.n6);
              Button b7 =  (Button) findViewById(R.id.n7);
              Button b8 =  (Button) findViewById(R.id.n8);
              Button b9 =  (Button) findViewById(R.id.n9);
        	count = count+1;
        	TextView  tv = (TextView) findViewById(R.id.TextView01);
        	check(b1,b2,b3,b4,b5,b6,b7,b8,b9, tv);
        	
        	
   
        	
	
        }
    };
    private OnClickListener b2Listener = new OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked
       	 Button  b1 =  (Button) findViewById(R.id.n1);
         Button b2 =  (Button) findViewById(R.id.n2);
         Button b3 =  (Button) findViewById(R.id.n3);
         Button b4 =  (Button) findViewById(R.id.n4);
         Button b5 =  (Button) findViewById(R.id.n5);
         Button b6 =  (Button) findViewById(R.id.n6);
         Button b7 =  (Button) findViewById(R.id.n7);
         Button b8 =  (Button) findViewById(R.id.n8);
         Button b9 =  (Button) findViewById(R.id.n9);
       	b2.setText(xoro());
       	count = count+1;
       	TextView  tv = (TextView) findViewById(R.id.TextView01);
       	check(b1,b2,b3,b4,b5,b6,b7,b8,b9, tv);
       	
	
        }
    };
    private OnClickListener b3Listener = new OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked
       	 Button  b1 =  (Button) findViewById(R.id.n1);
         Button b2 =  (Button) findViewById(R.id.n2);
         Button b3 =  (Button) findViewById(R.id.n3);
         Button b4 =  (Button) findViewById(R.id.n4);
         Button b5 =  (Button) findViewById(R.id.n5);
         Button b6 =  (Button) findViewById(R.id.n6);
         Button b7 =  (Button) findViewById(R.id.n7);
         Button b8 =  (Button) findViewById(R.id.n8);
         Button b9 =  (Button) findViewById(R.id.n9);
       	b3.setText(xoro());
       	count = count+1;
       	TextView  tv = (TextView) findViewById(R.id.TextView01);
       	check(b1,b2,b3,b4,b5,b6,b7,b8,b9, tv);
       	
	
        }
    };
    private OnClickListener b4Listener = new OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked
          	 Button  b1 =  (Button) findViewById(R.id.n1);
            Button b2 =  (Button) findViewById(R.id.n2);
            Button b3 =  (Button) findViewById(R.id.n3);
            Button b4 =  (Button) findViewById(R.id.n4);
            Button b5 =  (Button) findViewById(R.id.n5);
            Button b6 =  (Button) findViewById(R.id.n6);
            Button b7 =  (Button) findViewById(R.id.n7);
            Button b8 =  (Button) findViewById(R.id.n8);
            Button b9 =  (Button) findViewById(R.id.n9);
          	b4.setText(xoro());
          	count = count+1;
          	TextView  tv = (TextView) findViewById(R.id.TextView01);
          	check(b1,b2,b3,b4,b5,b6,b7,b8,b9, tv);
  
	
        }
    };
    private OnClickListener b5Listener = new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked 	
            // do something when the button is clicked
          	 Button  b1 =  (Button) findViewById(R.id.n1);
            Button b2 =  (Button) findViewById(R.id.n2);
            Button b3 =  (Button) findViewById(R.id.n3);
            Button b4 =  (Button) findViewById(R.id.n4);
            Button b5 =  (Button) findViewById(R.id.n5);
            Button b6 =  (Button) findViewById(R.id.n6);
            Button b7 =  (Button) findViewById(R.id.n7);
            Button b8 =  (Button) findViewById(R.id.n8);
            Button b9 =  (Button) findViewById(R.id.n9);
          	b5.setText(xoro());
          	count = count+1;
          	TextView  tv = (TextView) findViewById(R.id.TextView01);
          	check(b1,b2,b3,b4,b5,b6,b7,b8,b9, tv);

	
        }
    };
    private OnClickListener b6Listener = new OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked
          	 Button  b1 =  (Button) findViewById(R.id.n1);
            Button b2 =  (Button) findViewById(R.id.n2);
            Button b3 =  (Button) findViewById(R.id.n3);
            Button b4 =  (Button) findViewById(R.id.n4);
            Button b5 =  (Button) findViewById(R.id.n5);
            Button b6 =  (Button) findViewById(R.id.n6);
            Button b7 =  (Button) findViewById(R.id.n7);
            Button b8 =  (Button) findViewById(R.id.n8);
            Button b9 =  (Button) findViewById(R.id.n9);
          	b6.setText(xoro());
          	count = count+1;
          	TextView  tv = (TextView) findViewById(R.id.TextView01);
          	check(b1,b2,b3,b4,b5,b6,b7,b8,b9, tv);
	
        }
    };
    private OnClickListener b7Listener = new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked 	
            // do something when the button is clicked
          	 Button  b1 =  (Button) findViewById(R.id.n1);
            Button b2 =  (Button) findViewById(R.id.n2);
            Button b3 =  (Button) findViewById(R.id.n3);
            Button b4 =  (Button) findViewById(R.id.n4);
            Button b5 =  (Button) findViewById(R.id.n5);
            Button b6 =  (Button) findViewById(R.id.n6);
            Button b7 =  (Button) findViewById(R.id.n7);
            Button b8 =  (Button) findViewById(R.id.n8);
            Button b9 =  (Button) findViewById(R.id.n9);
          	b7.setText(xoro());
          	count = count+1;
          	TextView  tv = (TextView) findViewById(R.id.TextView01);
          	check(b1,b2,b3,b4,b5,b6,b7,b8,b9, tv);
	
        }
    };
    private OnClickListener b8Listener = new OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked
          	 Button  b1 =  (Button) findViewById(R.id.n1);
            Button b2 =  (Button) findViewById(R.id.n2);
            Button b3 =  (Button) findViewById(R.id.n3);
            Button b4 =  (Button) findViewById(R.id.n4);
            Button b5 =  (Button) findViewById(R.id.n5);
            Button b6 =  (Button) findViewById(R.id.n6);
            Button b7 =  (Button) findViewById(R.id.n7);
            Button b8 =  (Button) findViewById(R.id.n8);
            Button b9 =  (Button) findViewById(R.id.n9);
            
          	b8.setText(xoro());
          	count = count+1;
          	TextView  tv = (TextView) findViewById(R.id.TextView01);
          	check(b1,b2,b3,b4,b5,b6,b7,b8,b9, tv);

	
        }
    };
    private OnClickListener b9Listener = new OnClickListener() {
        public void onClick(View v) {
            // do something when the button is clicked
          	 Button  b1 =  (Button) findViewById(R.id.n1);
            Button b2 =  (Button) findViewById(R.id.n2);
            Button b3 =  (Button) findViewById(R.id.n3);
            Button b4 =  (Button) findViewById(R.id.n4);
            Button b5 =  (Button) findViewById(R.id.n5);
            Button b6 =  (Button) findViewById(R.id.n6);
            Button b7 =  (Button) findViewById(R.id.n7);
            Button b8 =  (Button) findViewById(R.id.n8);
            Button b9 =  (Button) findViewById(R.id.n9);
          	b9.setText(xoro());
          	count = count+1;
          	TextView  tv = (TextView) findViewById(R.id.TextView01);
          	check(b1,b2,b3,b4,b5,b6,b7,b8,b9, tv);
	
        }
    };
    public static String xoro ()
    {
    
       if (count%2==0)
       {
          return "O";}
       return "X";
    }
    public static void check(Button b1,Button b2,Button b3,Button b4,Button b5,Button b6,Button b7,Button b8,Button b9,TextView tv)
    {
    	  if ((b1.getText().toString()+b2.getText().toString()+b3.getText().toString()).equalsIgnoreCase("XXX") )
    	  {
    		  tv.setText("X Has Won") ;
    	  }
    	  else
    		  if ((b1.getText().toString()+b2.getText().toString()+b3.getText().toString()).equalsIgnoreCase("OOO") )
        	  {
        		  tv.setText("O Has Won") ;
        	  }
    		  else
    			  if ((b4.getText().toString()+b5.getText().toString()+b6.getText().toString()).equalsIgnoreCase("XXX") )
    	    	  {
    	    		  tv.setText("X Has Won") ;
    	    	  }
    	    	  else
    	    		  if ((b4.getText().toString()+b5.getText().toString()+b7.getText().toString()).equalsIgnoreCase("OOO") )
    	        	  {
    	        		  tv.setText("O Has Won") ;
    	        	  }
    	    		  else
    	    			  if ((b7.getText().toString()+b8.getText().toString()+b9.getText().toString()).equalsIgnoreCase("XXX") )
    	    	    	  {
    	    	    		  tv.setText("X Has Won") ;
    	    	    	  }
    	    	    	  else
    	    	    		  if ((b7.getText().toString()+b8.getText().toString()+b9.getText().toString()).equalsIgnoreCase("OOO") )
    	    	        	  {
    	    	        		  tv.setText("O Has Won") ;
    	    	        	  }
    	    	    		  else
    	    	    			  if ((b1.getText().toString()+b5.getText().toString()+b9.getText().toString()).equalsIgnoreCase("XXX") )
    	    	    	    	  {
    	    	    	    		  tv.setText("X Has Won") ;
    	    	    	    	  }
    	    	    	    	  else
    	    	    	    		  if ((b1.getText().toString()+b5.getText().toString()+b9.getText().toString()).equalsIgnoreCase("OOO") )
    	    	    	        	  {
    	    	    	        		  tv.setText("O Has Won") ;
    	    	    	        	  }
    	    	    	    		  else
    	    	    	    			  if ((b3.getText().toString()+b5.getText().toString()+b8.getText().toString()).equalsIgnoreCase("XXX") )
    	    	    	    	    	  {
    	    	    	    	    		  tv.setText("X Has Won") ;
    	    	    	    	    	  }
    	    	    	    	    	  else
    	    	    	    	    		  if ((b3.getText().toString()+b5.getText().toString()+b7.getText().toString()).equalsIgnoreCase("OOO") )
    	    	    	    	        	  {
    	    	    	    	        		  tv.setText("O Has Won") ;
    	    	    	    	        	  }
    	    	    	    	    		  else
    	    	    	    	    			  if ((b1.getText().toString()+b4.getText().toString()+b7.getText().toString()).equalsIgnoreCase("XXX") )
    	    	    	    	    	    	  {
    	    	    	    	    	    		  tv.setText("X Has Won") ;
    	    	    	    	    	    	  }
    	    	    	    	    	    	  else
    	    	    	    	    	    		  if ((b1.getText().toString()+b4.getText().toString()+b7.getText().toString()).equalsIgnoreCase("OOO") )
    	    	    	    	    	        	  {
    	    	    	    	    	        		  tv.setText("O Has Won") ;
    	    	    	    	    	        	  }
    	    	    	    	    	    		  else
    	    	    	    	    	    			  if ((b3.getText().toString()+b6.getText().toString()+b9.getText().toString()).equalsIgnoreCase("XXX") )
    	    	    	    	    	    	    	  {
    	    	    	    	    	    	    		  tv.setText("X Has Won") ;
    	    	    	    	    	    	    	  }
    	    	    	    	    	    	    	  else
    	    	    	    	    	    	    		  if ((b3.getText().toString()+b6.getText().toString()+b9.getText().toString()).equalsIgnoreCase("OOO") )
    	    	    	    	    	    	        	  {
    	    	    	    	    	    	        		  tv.setText("O Has Won") ;
    	    	    	    	    	    	        	  }
    	  
    }

 
    

}